Use getopt.gnu_getopt rather than getopt.getopt, so that xm list VM --long is
authoremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Fri, 2 Dec 2005 01:04:09 +0000 (01:04 +0000)
committeremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Fri, 2 Dec 2005 01:04:09 +0000 (01:04 +0000)
parsed the same as xm list --long VM.  This is only in Python 2.3 -- for
Python 2.2 we keep the old getopt.getopt behaviour.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
tools/python/xen/xm/main.py
tools/python/xen/xm/opts.py

index bd4d03da4786ae2c34120eabd094f5779fbe6d6a..83c1bc47944c80ce5e1a15862a2fb5f83a26ad5c 100644 (file)
@@ -25,7 +25,7 @@ import os
 import os.path
 import sys
 import re
-from getopt import getopt
+import getopt
 import socket
 import warnings
 warnings.filterwarnings('ignore', category=FutureWarning)
@@ -39,6 +39,14 @@ from xen.xm.opts import *
 
 import console
 
+
+# getopt.gnu_getopt is better, but only exists in Python 2.3+.  Use
+# getopt.getopt if gnu_getopt is not available.  This will mean that options
+# may only be specified before positional arguments.
+if not hasattr(getopt, 'gnu_getopt'):
+    getopt.gnu_getopt = getopt.getopt
+
+
 # Strings for shorthelp
 console_help = "console <DomId>                  Attach to domain DomId's console."
 create_help =  """create [-c] <ConfigFile>
@@ -332,8 +340,8 @@ def xm_list(args):
     use_long = 0
     show_vcpus = 0
     try:
-        (options, params) = getopt(args, 'lv', ['long','vcpus'])
-    except GetoptError, opterr:
+        (options, params) = getopt.gnu_getopt(args, 'lv', ['long','vcpus'])
+    except getopt.GetoptError, opterr:
         err(opterr)
         sys.exit(1)
     
@@ -729,8 +737,8 @@ def xm_network_detach(args):
 def xm_vnet_list(args):
     from xen.xend.XendClient import server
     try:
-        (options, params) = getopt(args, 'l', ['long'])
-    except GetoptError, opterr:
+        (options, params) = getopt.gnu_getopt(args, 'l', ['long'])
+    except getopt.GetoptError, opterr:
         err(opterr)
         sys.exit(1)
     
index 3f983407fc439d71711ad7d6095f26325eafd557..e75af798ae1c7e216b877fed8ead65e9286c0717 100644 (file)
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #============================================================================
 # Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
+# Copyright (C) 2005 XenSource Ltd.
 #============================================================================
 
 """Object-oriented command-line option support.
 """
-from getopt import getopt, GetoptError
+import getopt
 import os
 import os.path
 import sys
@@ -333,9 +334,10 @@ class Opts:
         while args:
             # let getopt parse whatever it feels like -- if anything
             try:
-                (xvals, args) = getopt(args[0:],
-                                       self.short_opts(), self.long_opts())
-            except GetoptError, err:
+                (xvals, args) = getopt.getopt(args[0:],
+                                              self.short_opts(),
+                                              self.long_opts())
+            except getopt.GetoptError, err:
                 self.err(str(err))
                 
             for (k, v) in xvals: